<template>
  <div>

    <sky-panel title="成本测算">
      <template #header>
        <span>结合树形表格组件实现成本测算功能</span>
      </template>
      <template #main>

        <sky-tree-table
          ref="skyTable"
          v-slot="row"
          :data="data"
          node-key="id"
          fixed-head
          :max-height="350"
          :props="defaultProps"
          @getCurrent="getCurrent"
        >
          <sky-tree-column :row="row?.scope" prop="name" label="科目名称" width="350" ladder fixed fixed-left-width="0">{{ row?.scope?.data.name }}</sky-tree-column>
          <sky-tree-column :row="row?.scope" prop="planAmountTax" label="规划金额(含税)" align="right">
            <span v-if="row.scope.childNodes.length>0">{{ toThousandDecimalFilter(row.scope.data.planAmountTax) }}</span>
            <el-input v-else v-model="row.scope.data.planAmountTax" v-thousands.planAmountTax="row.scope.data" @blur="planAmountTaxChange(row.scope,row.scope.data.planAmountTax)" />
          </sky-tree-column>
          <sky-tree-column :row="row?.scope" prop="planAmount" label="规划金额(不含税)" align="right">
            <span v-if="row.scope.childNodes.length>0">{{ toThousandDecimalFilter(row.scope.data.planAmount) }}</span>
            <el-input v-else v-model="row.scope.data.planAmount" v-thousands.planAmount="row.scope.data" @blur="planAmountChange(row.scope,row.scope.data.planAmount)" />
          </sky-tree-column>
          <sky-tree-column :row="row?.scope" prop="rate" label="税率">
            <el-input v-if="row.scope.childNodes.length==0" v-model="row.scope.data.rate" v-rate.rate="row.scope.data" @blur="rateChange(row.scope,row.scope.data.rate)" />
          </sky-tree-column>
        </sky-tree-table>

      </template>
    </sky-panel>

  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import _ from 'lodash'

export default defineComponent({
  setup() {
    const data = ref([
      {
        id: '1',
        name: '项目成本',
        planAmountTax: 80000,
        planAmount: 56000,
        rate: 0.7,
        children: [
          {
            id: '2-1',
            name: '开发成本',
            planAmountTax: 80000,
            planAmount: 56000,
            rate: 0.7,
            children: [
              {
                id: '3-1',
                name: '土地费',
                planAmountTax: 60000,
                planAmount: 42000,
                rate: 0.7,
                children: [
                  {
                    id: '4-1',
                    name: '土地成本',
                    planAmountTax: 60000,
                    planAmount: 42000,
                    rate: 0.7,
                    children: [
                      {
                        id: '5-1',
                        name: '土地出让金/购楼价款',
                        planAmountTax: 20000,
                        planAmount: 14000,
                        rate: 0.7
                      },
                      {
                        id: '5-2',
                        name: '土地契税',
                        planAmountTax: 20000,
                        planAmount: 14000,
                        rate: 0.7
                      },
                      {
                        id: '5-4',
                        name: '拆迁补偿费',
                        planAmountTax: 20000,
                        planAmount: 14000,
                        rate: 0.7
                      }
                    ]
                  }
                ]
              }, {
                id: '3-2',
                name: '前期费',
                planAmountTax: 20000,
                planAmount: 14000,
                rate: 0.7,
                children: [
                  {
                    id: '4-1-1',
                    name: '土地出让金/购楼价款',
                    planAmountTax: 20000,
                    planAmount: 14000,
                    rate: 0.7,
                    children: [
                      {
                        id: '4-1-1-1',
                        name: '城市建设配套费',
                        planAmountTax: 20000,
                        planAmount: 14000,
                        rate: 0.7
                      }
                    ]
                  }]
              }

            ]
          }
        ]
      }
    ])

    const planAmountTaxChange = (row: any, value: any) => {
      row.data['planAmount'] = _.multiply(value, row.data['rate'])
      const summary = (row:any) => {
        row.parent.data['planAmount'] = 0
        row.parent.data['planAmountTax'] = 0
        row.parent.data.children && row.parent.data.children.map((item: { [x: string]: any }) => {
          row.parent.data['planAmount'] = _.add(row.parent.data['planAmount'], parseFloat(item['planAmount']))
          row.parent.data['planAmountTax'] = _.add(row.parent.data['planAmountTax'], parseFloat(item['planAmountTax']))
        })
        row.parent.parent != null && summary(row.parent)
      }
      row.parent != null && summary(row)
    }

    const planAmountChange = (row: any, value: any) => {
      row.data['planAmountTax'] = _.divide(value, row.data['rate'])
      const summary = (row:any) => {
        row.parent.data['planAmount'] = 0
        row.parent.data['planAmountTax'] = 0
        row.parent.data.children && row.parent.data.children.map((item: { [x: string]: any }) => {
          row.parent.data['planAmount'] = _.add(row.parent.data['planAmount'], parseFloat(item['planAmount']))
          row.parent.data['planAmountTax'] = _.add(row.parent.data['planAmountTax'], parseFloat(item['planAmountTax']))
        })
        row.parent.parent != null && summary(row.parent)
      }
      row.parent != null && summary(row)
    }

    const rateChange = (row: any, value: any) => {
      row.data['planAmount'] = _.multiply(value, row.data['planAmountTax'])

      const summary = (row:any) => {
        row.parent.data['planAmount'] = 0
        row.parent.data['planAmountTax'] = 0
        row.parent.data.children && row.parent.data.children.map((item: { [x: string]: any }) => {
          row.parent.data['planAmount'] = _.add(row.parent.data['planAmount'], parseFloat(item['planAmount']))
          row.parent.data['planAmountTax'] = _.add(row.parent.data['planAmountTax'], parseFloat(item['planAmountTax']))
        })
        row.parent.parent != null && summary(row.parent)
      }
      row.parent != null && summary(row)
    }

    return { data, planAmountTaxChange, planAmountChange, rateChange }
  }
})
</script>

<style scoped>

</style>